package com.yuzhi.system.domain;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @Author Mario
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(exclude = "parent")
public class Area implements Serializable {

    @EqualsAndHashCode.Include
    @JsonAlias("id")
    private String code;

    private String name;

    private String pid;

    @JsonAlias("deep")
    private int level;

    private String pinyin;

    @JsonAlias("pinyin_prefix")
    private String pinyinPrefix;
    @JsonAlias("short_name")
    private String shortName;

    private String longitude;

    private String latitude;

    @JsonAlias("ext_name")
    private String extName;

    private List<String> names;

    private List<String> codes;

    @JsonAlias("childs")
    private List<Area> children;

    @JsonIgnore
    private Area parent;

    public String getFullName(String delimiter) {
        if (CollectionUtils.isEmpty(names)) {
            return name;
        }
        return String.join(delimiter, names);
    }

    public boolean isProvince() {
        return level == 0;
    }

    public boolean isCity() {
        return level == 1;
    }

    public boolean isDistrict() {
        return level == 2;
    }

}
